package com.woniu.liuzhougov.itemAndTask.controller;


import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.authorityControl.service.UserService;
import com.woniu.liuzhougov.itemAndTask.pojo.*;
import com.woniu.liuzhougov.itemAndTask.service.AreaService;
import com.woniu.liuzhougov.itemAndTask.service.ItemTypeService;
import com.woniu.liuzhougov.itemAndTask.service.ItemsManageService;
import com.woniu.liuzhougov.itemAndTask.service.UrgencyService;
import com.woniu.liuzhougov.itemAndTask.vo.AddItemVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 事项管理
 */
@Controller
@RequestMapping("itemsManage")
public class ItemsManageController {

    @Autowired
    private ItemsManageService itemsManageService;
    @Autowired
    private UserService userService;
    @Autowired
    private AreaService areaService;
    @Autowired
    private ItemTypeService itemTypeService;
    @Autowired
    private UrgencyService urgencyService;


    /**
     * 分页 查询 所有区级事项
     * @return
     */
    @RequestMapping("/listItems")
    @ResponseBody
    public Map listItems(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();   //获取登录信息
        List<ItemsPojo> list = itemsManageService.allItms(user.getDepartment().getDepartmentId(),user.getArea().getAreaId());
        Map<String,Object> map = new HashMap<>();
        map.put("count",list.size());
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        return map;
    }
    /**
     * 分页 查询 所有市级事项
     * @return
     */
    @RequestMapping("/listSItems")
    @ResponseBody
    public Map listSItems(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();   //获取登录信息
        List<ItemsPojo> list = itemsManageService.allSItms(user.getDepartment().getDepartmentId(),user.getArea().getAreaId());
        Map<String,Object> map = new HashMap<>();
        map.put("count",list.size());
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        return map;
    }



    /**
     * 删除事项
     * @param sxId
     * @return
     */
    @RequestMapping("/deleteItem")
    @ResponseBody
    @RequiresPermissions(value = "delete_item")
    public String deleteItem(Integer sxId){
        String result = itemsManageService.deleteItemBySxID(sxId);
        if(result.equals("success")){
            return "success";
        }
        if(result.equals("hasTask")){
            return "hasTask";
        }
        return "loss";
    }


    /**
     * 查询所有事项(地区、紧急情况、事项类型) 分类
     * @return
     */
    @RequestMapping("/searchAllType")
    @ResponseBody
    public Map searchAllType(){
        Map<String,Object> map = new HashMap<>();
        List<Urgenry> urgency = urgencyService.searchAllTypeOfUrgenry();
        List<ItemType> itemType = itemTypeService.searchAllTypeOfItemType();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Area area = user.getArea();
        map.put("area",area);
        map.put("urengcy", urgency);
        map.put("itemType",itemType);
        return map;
    }


    /**
     * 添加事项
     * @param vo
     * @return
     */
    @RequestMapping("/addItem")
    @ResponseBody
    @RequiresPermissions(value = "add_item")
    public String addItem(AddItemVo vo){
        User user = (User) SecurityUtils.getSubject().getPrincipal();  //从shiro中获取登录用户名称
        vo.setItemFounderId(user.getUserId());
        vo.setItemStatusId(1);
        vo.setDepartmentId(user.getDepartment().getDepartmentId());
        if(vo.getItemArea() != user.getArea().getAreaId()){
            return "areaError";
        }
        boolean b = itemsManageService.insertNewItem(vo);
        if(b){
            return "success";
        }
        return "loss";
    }


    /**
     * 根据ID查询某个事项的详情
     * @param ItemId
     * @return
     */
    @RequestMapping("/searchItemById")
    @ResponseBody
    public ItemsPojo searchItemById(Integer ItemId){
        ItemsPojo itemsPojo = itemsManageService.searchItemByItemId(ItemId);
        return itemsPojo;
    }


    /**
     * 修改事项
     * @param itemId
     * @param editItemTitle
     * @param editItemContent
     * @param editItemRemark
     * @param editItemUrgency
     * @return
     */
    @RequestMapping("/updateItemById")
    @ResponseBody
    @RequiresPermissions(value = "change_item")
    public String updateItemById(Integer itemId,String editItemTitle,String editItemContent,String editItemRemark,Integer editItemUrgency){
        boolean b = itemsManageService.updateItemById(editItemContent,editItemRemark,editItemTitle,editItemUrgency,itemId);
        if(b){
            return "success";
        }
        return "loss";
    }


    /**
     * 查询所有  我的事项
     * @return
     */
    @RequestMapping("/listMyItems")
    @ResponseBody
    public Map listMyItems(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();   //获取登录信息
        List<ItemsPojo> list = itemsManageService.listMyItems(user.getUserId());
        Map<String,Object> map = new HashMap<>();
        map.put("count",list.size());
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        return map;
    }


    /**
     * 查看所有 待审批事项
     * @return
     */
    @RequestMapping("/listAwaitingApprovalItems")
    @ResponseBody
    public Map listAwaitingApprovalItems(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();   //获取登录信息
        List<ItemsPojo> list = itemsManageService.listAwaitingApprovalItems(user.getDepartment().getDepartmentId(),user.getArea().getAreaId());
        Map<String,Object> map = new HashMap<>();
        map.put("count",list.size());
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

}
